package main

import (
	"io"
	"net/http"
	"net/rpc"
	"net/rpc/jsonrpc"
)

type HelloRPC struct{}

func (h *HelloRPC) Hello(request string, resp *string) error {
	*resp = "hello, " + request
	return nil
}

func main() {
	rpc.RegisterName("HelloService", &HelloRPC{})
	// 启动服务
	http.HandleFunc("/jsonrpc", func(w http.ResponseWriter, r *http.Request) {
		var conn io.ReadWriteCloser = struct {
			io.Writer
			io.ReadCloser
		}{
			ReadCloser: r.Body,
			Writer:     w,
		}
		rpc.ServeCodec(jsonrpc.NewServerCodec(conn))
	})

	http.ListenAndServe(":8080", nil)
}
